package com.ligongmusic.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Pattern;

public class StringUtils {

    /**
     * 验证是否合法的手机号
     *
     * @param phone
     * @return
     */
    public static boolean isMobile(String phone) {
        return Pattern.matches("^((13[0-9])|(14[5,7,9])|(15[0-9])|(17[0,1,3,5-8])|(18[0-9]))\\d{8}$", phone);
    }

    public static String ms2Date(String ms) {
        if ("".equals(ms) || "null".equals(ms) || ms == null)
            return null;
        return ms2Date(Long.parseLong(ms));
    }


    public static String ms2Date(long ms) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        return formatter.format(calendar.getTime());
    }


    public static String getYear(long ms) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        return formatter.format(calendar.getTime());
    }

    public static String getYear(String ms) {
        if ("".equals(ms) || "null".equals(ms) || ms == null)
            return null;
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.parseLong(ms));
        return formatter.format(calendar.getTime());
    }

    public static String getTime(long ms) {
        DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        return formatter.format(calendar.getTime());
    }

    public static String getMusicMax(long ms) {
        DateFormat formatter = new SimpleDateFormat("mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        return formatter.format(calendar.getTime());
    }

    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str) || "null".equals(str))
            return true;
        return false;
    }

    /**
     * 检测String是否全是中文
     *
     * @param name
     * @return
     */
    public static boolean checkNameChese(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();
        for (int i = 0; i < name.length(); i++) {
            if (!isChinese(cTemp[i])) {
                res = false;
                break;
            }
        }
        return res;
    }

    /**
     * 判定输入汉字
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
}
